package service;

import org.dom4j.Document;

/**
 * WebService Router
 * 
 * @author Maurice & Topia
 */
public class Router {

	private Router() {
		// nothing
	}

	private static Router singleton;

	public static Router getInstance() {
		if (singleton == null) {
			singleton = new Router();
		}
		return singleton;
	}

	public Document getResultFor(int token) {
		Document d = BusESB.getInstance().getResultat(token);
		if (d == null) {
			System.err.println("(Router) Result " + token + " is not available yet");
		}
		return d;
	}

	public Document invoke(String XMLClientData, String idBusinessProcess) {
		return BusESB.getInstance().getBusinessProcess(idBusinessProcess).execute(XMLClientData);
	}

	public int invokeOneWay(final String XMLClientData, final String idBusinessProcess) {
		final int token = BusESB.getInstance().getFreeToken();

		new Thread() {
			@Override
			public void run() {
				Document d = BusESB.getInstance().getBusinessProcess(idBusinessProcess).execute(XMLClientData);
				BusESB.getInstance().addResultat(token, d);
			}
		}.start();

		return token;
	}
}
